// templates/salesman/pages/location/location.js
import Notify from '@vant/weapp/notify/notify';
import {clearUserLocalStorage} from "../../../../utils/util";
const app = getApp();

const currentDate = new Date();

// 获取当前月份的数字（注意：月份从0开始，0 表示一月，11 表示十二月）
const currentMonth = currentDate.getMonth() + 1;


Page({

    /**
     * 页面的初始数据
     */
    data: {

        current_month: currentMonth,

        current_month_info: {},
        current_month_new_info:  {},
        prev_month_info:  {},
        prev_month_new_info:  {},
        prev_full_month_info: {},

        share_options: [
            { text: '全部销售', value: 0 },
        ],
        current_share_id: 0,


        current_month_text: '',
        prev_month_text: '',
        month_options: [
            { text: '一月', value: 1 },
            { text: '二月', value: 2 },
            { text: '三月', value: 3 },
            { text: '四月', value: 4 },
            { text: '五月', value: 5 },
            { text: '六月', value: 6 },
            { text: '七月', value: 7 },
            { text: '八月', value: 8 },
            { text: '九月', value: 9 },
            { text: '十月', value: 10 },
            { text: '十一月', value: 11 },
            { text: '十二月', value: 12 },
        ],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this
        let now = (new Date().getTime()) / 1000;
        let expiration_time = wx.getStorageSync('expiration_time')
        let group = wx.getStorageSync('role')

        if (group!=='销售员') {
            clearUserLocalStorage()
            expiration_time = 0
        }



        if (now > expiration_time) {
            wx.reLaunch({
                url: '/pages/login/login',
            })
        }

        let access_token =  wx.getStorageSync('access_token')
        let user = wx.getStorageSync('user')
        let data ={
            'access_token': access_token,
            'user': user,
            'level': user.level,
        }

        let current_share_id = wx.getStorageSync('current_share_id')
        if (current_share_id) {
            data['current_share_id'] = current_share_id
        }
        this.setData(data)
        this.get_statistics().then(r => {})

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    on_dropdown_month_change(e) {
        let current_month = e.detail
        console.log('--------on_dropdown_month_change:', current_month)
        this.setData({
            current_month: current_month
        })
        wx.setStorageSync('current_month', current_month)
        this.get_statistics().then(r => {})
    },
    on_dropdown_share_id_change(e) {
        let current_share_id = e.detail
        console.log('--------on_dropdown_share_id_change:', current_share_id)
        this.setData({
            current_share_id: current_share_id
        })
        wx.setStorageSync('current_share_id', current_share_id)
        this.get_statistics().then(r => {})
    },

    async get_share_list() {
        const that = this
        wx.request({
            url: app.globalData.API + '/sales/index/get-share-list',
            header: {
                'Content-Type': 'application/json',
                "x-api-key": this.data.access_token,
            },
            method: "POST",
            data: {
                region: '[E区]',
            },
            success:(res) => {
                if (res.data.code === 200) {

                    let share_options = res.data.data
                    let default_options = [{'value':0, 'text':'全部'}]
                    that.setData({
                        share_options: default_options.concat(share_options)
                    })
                } else if (res.data.code === 0) {
                    clearUserLocalStorage()
                    wx.reLaunch({
                        url: "/pages/login/login"
                    })
                }
            }
        })


    },

    async get_statistics() {
        let that = this
        wx.request({
            url: app.globalData.API + '/sales/index/get-statistics',
            header: {
                'Content-Type': 'application/json',
                "x-api-key": this.data.access_token,
            },
            method: "POST",
            data: {
                current_share_id: that.data.current_share_id,
                current_month: that.data.current_month,
            },
            success:(res) => {
                if (res.data.code === 200) {

                    let data = res.data.data.valid_cps_status

                    let current_month_info = res.data.data.current_month_info
                    let current_month_new_info = res.data.data.current_month_new_info
                    let prev_month_info = res.data.data.prev_month_info
                    let prev_month_new_info = res.data.data.prev_month_new_info
                    let prev_full_month_info = res.data.data.prev_full_month_info

                    let current_month_index = that.data.current_month - 1
                    let current_month_text = ''
                    if (current_month_index > -1) {
                        current_month_text = that.data.month_options[current_month_index].text
                    }
                    let prev_month_index = current_month_index - 1
                    if (prev_month_index === -1) {
                        prev_month_index = 11
                    }
                    let prev_month_text = that.data.month_options[prev_month_index].text

                    console.log(prev_full_month_info)

                    that.setData({
                        current_month_text: current_month_text,
                        prev_month_text: prev_month_text,
                        current_month_info: current_month_info,
                        current_month_new_info: current_month_new_info,
                        prev_month_info: prev_month_info,
                        prev_month_new_info: prev_month_new_info,
                        prev_full_month_info: prev_full_month_info,


                    })

                } else if (res.data.code === 0) {
                    clearUserLocalStorage()
                    wx.reLaunch({
                        url: "/pages/login/login"
                    })
                }
            }
        })
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.get_share_list()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },



    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        let that = this
        setTimeout(function () {
            that.get_statistics().then(r => {

            })
        }, 2000);
        wx.stopPullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
})
